import { defHttp } from '/@/utils/http/axios';
import { DepartmentModel, DeptListItem } from '/@/api/sys/model/departmentModel';
import { ErrorMessageMode } from '/#/axios';

enum Api {
  GetDepartmentList = '/api/department',
  addDepartment = '/api/department',
  updateDepartment = '/api/department/<pk>',
  deleteDepartment = '/api/department/<pk>',
  exportDepartment = '/api/department/export',
}

export const getDepartmentList = (params?: DeptListItem, mode: ErrorMessageMode = 'modal') => {
  return defHttp.get<DepartmentModel>(
    {
      url: Api.GetDepartmentList,
      params,
    },
    {
      withToken: true,
      errorMessageMode: mode,
    },
  );
};

export const addDepartment = (params: DeptListItem, mode: ErrorMessageMode = 'modal') => {
  return defHttp.post<DeptListItem>(
    {
      url: Api.addDepartment,
      params,
    },
    {
      withToken: true,
      errorMessageMode: mode,
    },
  );
};

export const updateDepartment = (params: DeptListItem, mode: ErrorMessageMode = 'modal') => {
  return defHttp.put<DeptListItem>(
    {
      url: Api.updateDepartment.replace('<pk>', String(params.id)),
      params,
    },
    {
      withToken: true,
      errorMessageMode: mode,
    },
  );
};

export const deleteDepartment = (params: DeptListItem, mode: ErrorMessageMode = 'message') => {
  return defHttp.delete<DeptListItem>(
    {
      url: Api.updateDepartment.replace('<pk>', String(params.id)),
      params,
    },
    {
      withToken: true,
      errorMessageMode: mode,
    },
  );
};

export const exportDepartment = (id_list: number[], mode: ErrorMessageMode = 'message') => {
  return defHttp.post(
    {
      url: Api.exportDepartment,
      responseType: 'blob',
      params: {
        id_list,
      },
    },
    {
      withToken: true,
      errorMessageMode: mode,
      isTransformResponse: false,
      isReturnNativeResponse: true,
    },
  );
};
